package com.qbw.app;

import android.content.Context;
import androidx.multidex.MultiDexApplication;
import android.util.Log;

import com.baidu.mapapi.SDKInitializer;
import com.http.okhttp.HttpTask;
import com.image.fresco.FrescoUtil;
import com.lib.util.AppUtil;
import com.lib.util.DateTimeUtil;
import com.lib.util.ResUtil;
import com.qbw.app.model.HttpResponse;
import com.qbw.core.download.XDownloader;
import com.qbw.core.okhttp.HttpErrorLogic;
import com.qbw.core.okhttp.HttpParam;
import com.qbw.l.L;
import com.qbw.spm.P;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.DefaultRefreshFooterCreator;
import com.scwang.smartrefresh.layout.api.DefaultRefreshHeaderCreator;
import com.scwang.smartrefresh.layout.api.RefreshFooter;
import com.scwang.smartrefresh.layout.api.RefreshHeader;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.tencent.bugly.crashreport.CrashReport;

import java.io.File;

import cn.jpush.android.api.JPushInterface;

/*
 * Created by QBW on 2019/7/19
 */
public class XApplication extends MultiDexApplication implements XDownloader.Listener {

    private static XApplication sInstance;

    public static XApplication getInstance() {
        return sInstance;
    }

    private boolean isCheckUpdate = false;

    //static 代码段可以防止内存泄露
    static {
        //设置全局的Header构建器
        SmartRefreshLayout.setDefaultRefreshHeaderCreator(new DefaultRefreshHeaderCreator() {
            @Override
            public RefreshHeader createRefreshHeader(Context context, RefreshLayout layout) {
//                layout.setPrimaryColorsId(R.color.colorPrimary, android.R.color.white);//全局设置主题颜色
                return new ClassicsHeader(context);//.setTimeFormat(new DynamicTimeFormat("更新于 %s"));//指定为经典Header，默认是 贝塞尔雷达Header
            }
        });
        //设置全局的Footer构建器
        SmartRefreshLayout.setDefaultRefreshFooterCreator(new DefaultRefreshFooterCreator() {
            @Override
            public RefreshFooter createRefreshFooter(Context context, RefreshLayout layout) {
                //指定为经典Footer，默认是 BallPulseFooter
                return new ClassicsFooter(context).setDrawableSize(20);
            }
        });
    }


    @Override
    public void onCreate() {
        super.onCreate();
        sInstance = this;
        boolean isDC = Config.isDeveloperChannel() || BuildConfig.DEBUG;
        //Preference
        P.init(this, isDC);
        Config.init(onGetMainApplicationFlavor());
        //BUgly
        CrashReport.initCrashReport(this, "ebc1385e89", isDC);
        //友盟
        //MobclickAgent.setCatchUncaughtExceptions(false);
        //MobclickAgent.setDebugMode(isDC);
        //mob
       /* MobSDK.init(this, "23fa64fc893bd", "b5c8cfee2ba118cddaa7a1ab440144c2");
        if (!isDC) {
            ShareSDK.closeDebug();
        }*/
        //日志
        /*if(isDC) {
            XLog.setEnabled(isDC, Environment.getExternalStorageDirectory()+File
            .separator+"magicyo_log");
        } else {
            XLog.setEnabled(false);
        }*/
        L.GL.setEnabled(isDC);
        //工具
        ResUtil.init(this);
        //Fresco
        FrescoUtil.init(this);
        initHttp(false);
        //Date
        DateTimeUtil.init(this);
        //Baidu
        SDKInitializer.initialize(this);
        //qiniu
        //QiNiu.init(this, this);
        //下载
        XDownloader.init(this);
        XDownloader.getInstance().addListener(this);
        //jpush
//        JPushUtil.init(this);
        //监听网络
        //NetworkListenerManager.init(this);
        //极光推送
        JPushInterface.setDebugMode(true);
        JPushInterface.init(this);
        Log.e("1099", "run:--------->registrationId： "+JPushInterface.getRegistrationID(this) );

    }

    public void initHttp(boolean chinaVersion) {
        HttpTask.init(BuildConfig.DEBUG,
                      this,
                      Config.getUrl(),
                      HttpParam.getInstance(),
                      HttpErrorLogic.getInstance(), HttpResponse.class,
                      "");
    }

    protected String onGetMainApplicationFlavor() {
        return "";
    }

    /*@Override
    public String getAppRootDir() {
        return Config.ROOT_DIR;
    }*/

    @Override
    public boolean onDownload(XDownloader.Status status) {
        if (status instanceof XDownloader.Status.Success) {
            XDownloader.Task task = status.getTask();
            if (task.getType() == Constant.D_TYPE_UPDATE) {
                L.GL.i("open apk[%s]", task.getFullPath());
                AppUtil.openApk(this, new File(task.getFullPath()));
            }
        }
        return false;
    }

    public boolean isCheckUpdate() {
        return isCheckUpdate;
    }

    public void setCheckUpdate(boolean checkUpdate) {
        isCheckUpdate = checkUpdate;
    }

}
